package Service;

import Bean.Audit;
import Bean.Flight;
import Bean.Page;
import Bean.Ticket;
import Dao.AuditDao;
import Utils.DataUtil;
import Utils.UUIDUtils;

import javax.lang.model.element.NestingKind;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-03-12
 * Time: 21:45
 */
//改签
public class AuditService {
    private AuditDao auditDao = new AuditDao();
    private TicketService ticketService = new TicketService();
    private FlightService flightService = new FlightService();

    //用户申请改签 约定: 1:表示申请通过 ,2:表示用户没有购买该航班机票(旧的机票)
    //                3:表示用户想要购买新的航班不存在
    public int change(String userId, String originalFlight, String newFlight,String gqSeatNumber) {
        //先去验证用户是否有购买originalFlight这个航班的机票
        Ticket ticket  = ticketService.select(userId,originalFlight,gqSeatNumber);

        if(ticket == null) {
            return 2;
        }
        //验证新航班是否存在 -- 使用航班号进行
        Flight flight = flightService.selectByFlightId(newFlight);
        if(flight == null) {
            return 3;
        }
        //提交审核
        Audit audit = new Audit();
        audit.setAuditId(UUIDUtils.getId());
        audit.setUserId(userId);
        audit.setTicketId(ticket.getTicketId());
        audit.setOldFlightId(originalFlight);
        audit.setNewFlightId(newFlight);
        //这块新的座位号就是原来的座位号
        audit.setRequestedSeat(ticket.getSeatNumber());
        audit.setApplyTime(DataUtil.getTime());
        auditDao.insert(audit);
        System.out.println(1);
        //修改用户订单状态 -- 改签中
        Ticket ticket1 = new Ticket();
        ticket1.setUserId(userId);
        ticket1.setFlightId(originalFlight);
        ticket1.setStatus(Ticket.TicketStatus.改签中);

        return ticketService.update(ticket1);
    }

    public Page selectByUserId(int pageNo, int pageSize, String userId) {
        Page page = new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setTotalRecords(auditDao.size(userId));
        page.setList(auditDao.selectByUserId(pageNo,pageSize,userId));
        return page;
    }

    public boolean delete(String auditId) {
        return auditDao.delete(auditId);
    }

    public Page selectList(int pageNo, int pageSize) {
        Page page = new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setTotalRecords(auditDao.size());
        page.setList(auditDao.select(pageNo,pageSize));
        return page;
    }

    //通过改签申请
    public boolean auditPass(String auditId, String ticketId,String newFlightId) {
        //1.先将改签表中的状态设置为已通过
        Audit audit = new Audit();
        audit.setAuditId(auditId);
        audit.setAuditStatus(Audit.AuditStatus.已通过);
        auditDao.update(audit);
        //获取到原订单所有的信息
        Ticket ticket = ticketService.selectByTicketId(ticketId);
        //生成新的订单数据
        Ticket newTicket = new Ticket();
        newTicket.setTicketId(UUIDUtils.getId());
        newTicket.setUserId(ticket.getUserId());
        newTicket.setSeatNumber(ticket.getSeatNumber());
        newTicket.setFlightId(newFlightId);
        newTicket.setStatus(Ticket.TicketStatus.已预订);
        System.out.println("生成新的机票的信息为 : " + newTicket.toString());
        ticketService.insert(newTicket);
        //把旧的订单进行删除
        return ticketService.delece(ticket.getTicketId());
    }

    public boolean auditRefuse(String auditId, String ticketId) {
        //把申请表中的状态修改为已拒绝
        auditDao.delete(auditId);
        //将原来的机票修改为已预订
        Ticket ticket = new Ticket();
        ticket.setTicketId(ticketId);
        ticket.setStatus(Ticket.TicketStatus.已预订);
        return ticketService.updateByTicketId(ticket);
    }
}
